package com.socialuni.common.user.sdk.manage;

import com.socialuni.common.user.sdk.repository.CommonUserAccountRepository;
import com.socialuni.uniapp.sdk.constant.UniPlatformType;
import com.socialuni.uniapp.sdk.model.UniUnionIdRO;
import com.socialuni.uniapp.sdk.model.UniProviderLoginQO;
import com.socialuni.common.user.model.DO.CommonUserAccountDO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CommonUserAccountDOGet {
    @Resource
    CommonUserAccountRepository uniUserAccountRepository;

    public CommonUserAccountDO getAccountByUnionId(UniProviderLoginQO loginQO, UniUnionIdRO uniUnionIdRO) {
        String provider = loginQO.getProvider();
        String unionId = uniUnionIdRO.getUnionid();
        String platform = loginQO.getPlatform();

        CommonUserAccountDO accountDO = uniUserAccountRepository.findByProviderAndUnionId(provider, unionId);

        //根据unionId找不到，代表没有开通开放平台或者没注册
        if (accountDO == null) {
            //通过openId查询试试
            String openId = uniUnionIdRO.getOpenid();
            if (platform.equals(UniPlatformType.app)) {
                accountDO = uniUserAccountRepository.findByProviderAndAppOpenId(provider, openId);
            } else {
                accountDO = uniUserAccountRepository.findByProviderAndMpOpenId(provider, openId);
            }
        }
        return accountDO;
    }
}
